var editRoleUrl = null;

var addRole = function () {
    editRoleUrl = ctx + '/sys/role_add';

    $('#form_role_edit').form('clear');
    $('#form_role_edit select[name="role.type"]').val(1);
    $('#form_role_edit select[name="role.state"]').val(1);
    $('#dl_role_edit').dialog('open');
}

var updateRole = function () {
    editRoleUrl = ctx + '/sys/role_update';
    var selected = $('#dg_role').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的角色");
        return;
    }
    $('#form_role_edit input[name="role.id"]').val(selected.id);
    $('#form_role_edit input[name="role.name"]').val(selected.name);
    $('#form_role_edit select[name="role.type"]').val(selected.type);
    $('#form_role_edit select[name="role.state"]').val(selected.state);
    editRoleUrl = ctx + '/sys/role_update';
    $('#dl_role_edit').dialog('open');
}

var closeRoleEdit = function () {
    $('#dl_role_edit').dialog('close');
}

var submitRole = function () {
    $('#form_role_edit').form('submit', {
        url: editRoleUrl,
        success: function (data) {
            data = eval('(' + data + ')');
            closeRoleEdit();
            $('#dg_role').datagrid('reload');
            if (data && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
        }
    });
}

var removeRole = function () {
    var url = ctx + "/sys/role_delete";
    var selected = $('#dg_role').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的角色");
        return;
    }
    $.messager.confirm("警告", "您确定要删除角色“" + selected.name + "”吗？", function (r) {
        if (!r) {
            return;
        }
        $.post(url, {'role.id': selected.id}, function (data) {
            $('#dg_role').datagrid('reload');
            if (data && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
        });
    })
}

var searchRole = function () {
    $('#dg_role').datagrid('load', {
        'role.id': $('#searRoleParams input[name="role.id"]').val(),
        'role.name': $('#searRoleParams input[name="role.name"]').val(),
        'role.type': $('#searRoleParams select[name="role.type"]').val()

    });
}